Assignemnt #86 and 85th program
Code
///Name: Derrick Andreasen
///Period: 7
///Program name: 85th Program
///File name: Eigh5prog.java
///Date Finished:1/12/2016
// When I changed the i < message.length to a i <= message. length it didn't tell me how many A's I had in the message.
// If the message us box then message is three spaces long and the x lands on the second position.
// The for loop wont repeat when i <= message.length because the loop will become equal to the message.length
import java.util.Scanner;
import java.util.Random;
public class Eigh5prog
{
public static void main( String[] args )
{
Scanner kb = new Scanner(System.in);
System.out.print("What is your message? ");
String message = kb.nextLine();
System.out.println("\nYour message is " + message.length() + " characters long.");
System.out.println("The first character is at position 0 and is '" + message.charAt(0) + "'.");
int lastpos = message.length() - 1;
System.out.println("The last character is at position " + lastpos + " and is '" + message.charAt(lastpos) + "'.");
System.out.println("\nHere are all the characters, one at a time:\n");
for ( int i=0; i < message.length(); i++ )
{
System.out.println("\t" + i + " - '" + message.charAt(i) + "'");
}
int a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;
for ( int i=0; i< message.length(); i++ )
{
char letter = message.charAt(i);
if ( letter == 'a' || letter == 'A' )
{
a_count++;
}
if ( letter == 'e' || letter == 'E' )
{
e_count++;
}
if ( letter == 'i' || letter == 'I' )
{
i_count++;
}
if ( letter == 'o' || letter == 'O' )
{
o_count++;
}
if ( letter == 'u' || letter == 'U' )
{
u_count++;
}
}
System.out.println("\nYour message contains the letter 'a' " + a_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'e' " + e_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'i' " + i_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'o' " + o_count + " times. Isn't that interesting?");
System.out.println("\nYour message contains the letter 'u' " + u_count + " times. Isn't that interesting?");
}
}
Picture of the output